package com.dcy.znoa.controller.publiz;

import com.dcy.znoa.common.annotation.AuthenticationAPI;
import com.dcy.znoa.common.controller.BaseController;
import com.dcy.znoa.common.exception.BusinessException;
import com.dcy.znoa.common.util.pojo.JsonCommon;
import com.dcy.znoa.controller.publiz.model.place.AddPlaceModel;
import com.dcy.znoa.controller.publiz.model.place.UpdatePlaceModel;
import com.dcy.znoa.dao.publiz.CompanyDao;
import com.dcy.znoa.domain.Oper;
import com.dcy.znoa.domain.publiz.Company;
import com.dcy.znoa.domain.publiz.Place;
import com.dcy.znoa.service.publiz.PlaceService;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.validation.Valid;
import java.lang.reflect.InvocationTargetException;

import static com.dcy.znoa.common.util.pojo.JsonCommon.*;

/**
 * 场所 controller
 * 1 不允许修改场所的上级场所，
 * 2 不允许删除拥有下级场所的场所
 * 3 不允许删除拥有视频的场所
 * 4 视频可以修改与场所的绑定关系
 *
 * @author pengyu
 */
@Controller
@RequestMapping("place")
public class PlaceController extends BaseController {
    @Autowired
    PlaceService placeService;
    @Autowired
    CompanyDao companyDao;

    @AuthenticationAPI
    @ResponseBody
    @RequestMapping("/selectTreeByParkId")//查询园区下场所的树形结构
    public JsonCommon selectTreeByParkId(Long parkId) throws BusinessException, InvocationTargetException, IllegalAccessException {
        if (parkId == null) {
            return new JsonCommon(PARAM_NOT_FOUND_CODE);
        }
        Place place = placeService.selectTreeByParkId(parkId);
        return new JsonCommon(place);
    }

    @AuthenticationAPI
    @ResponseBody
    @RequestMapping("/selectTreeByPlaceId")//查询场所下场所的树形结构
    public JsonCommon selectTreeByPlaceId(Long placeId) throws BusinessException, InvocationTargetException, IllegalAccessException {
        if (placeId == null) {
            return new JsonCommon(PARAM_NOT_FOUND_CODE);
        }
        Place place = placeService.selectTreeByPlaceId(placeId);
        return new JsonCommon(place);
    }

    @AuthenticationAPI
    @ResponseBody
    @RequestMapping("/selectDefaultTree")//查询当前登录用户对应企业可以看到的树
    public JsonCommon selectDefaultTree() throws BusinessException, InvocationTargetException, IllegalAccessException {
        //1 只能园区管理员、物业管理员、企业管理员可见
        Integer operType = getCurrentUser().getOperType();
        if (operType == null) {
            return new JsonCommon("404", "登录用户的用户类型为空，请联系管理员");
        }
        Place place = null;
        Long compId = getCurrentUser().getCompId();
        switch (operType) {// 1：平台管理员；2：企业管理员；3：企业用户;4：园区管理员；5：物业管理员；6：物业普通员工
            case 1:
                return new JsonCommon("404", "对平台管理员暂不开放此接口");
            case 2://
                place = placeService.selectSeeTreeByCompId(compId);
                return new JsonCommon(place);
            case 4://暂时写死，园区ID就是1
                place = placeService.selectSeeTreeByParkId(1L);
                return new JsonCommon(place);
            case 5:
                place = placeService.selectSeeTreeByCompId(compId);
                return new JsonCommon(place);
            default:
                return new JsonCommon("404", "企业管理员、园区管理员、物业管理员可以调用此接口");
        }
    }


    @AuthenticationAPI
    @ResponseBody
    @RequestMapping("/insert")
    public JsonCommon insert(@Valid AddPlaceModel model, BindingResult bindingResult) throws BusinessException, InvocationTargetException, IllegalAccessException {
        if (bindingResult.hasErrors()) {
            return new JsonCommon("404", bindingResult.getFieldError().getDefaultMessage());
        }
        model.setCreateOperId(getCurrentUser().getOperId());
        Place cp = new Place();
        BeanUtils.copyProperties(cp, model);
        Oper oper = getCurrentUser();
        Integer operType =  oper.getOperType();
        if(operType==null){
            return new JsonCommon(INSERT_ERROR_CODE,"用户的operType为空，请联系管理员");
        }
        Long compId = oper.getCompId();
        // 1：平台管理员；2：企业管理员；3：企业用户;4：园区管理员；5：物业管理员；6：物业普通员工
        switch (operType){
            case 2:
                if(compId==null){
                    return new JsonCommon(INSERT_ERROR_CODE,"用户没有对应的企业");
                }
                cp.setPlaceType(2);//企业
                cp.setEntityId(compId);
                break;
            case 4://暂时写死
                cp.setPlaceType(1);
                if(compId==null){
                    cp.setEntityId(1L);// 用cp.setEntityId(compId);也可以，因为用户类型为园区管理员时，这个compId 就是园区id 了
                }else{
                    cp.setEntityId(compId);
                }
                break;
            case 5:
                if(compId==null){
                    return new JsonCommon(INSERT_ERROR_CODE,"用户没有对应的物业");
                }
                cp.setPlaceType(2);//物业
                cp.setEntityId(compId);
                break;
            default:
                return new JsonCommon(INSERT_ERROR_CODE,"平台管理员和普通用户不开放此功能");
        }
        Integer result = placeService.insert(cp);
        if (result > 0) {
            return new JsonCommon(SUCCESS_CODE);
        }
        return new JsonCommon(INSERT_ERROR_CODE);
    }

    @AuthenticationAPI
    @ResponseBody
    @RequestMapping("/updateById")
    public JsonCommon updateById(@Valid UpdatePlaceModel model, BindingResult bindingResult) throws BusinessException, InvocationTargetException, IllegalAccessException {
        if (bindingResult.hasErrors()) {
            return new JsonCommon("404", bindingResult.getFieldError().getDefaultMessage());
        }
        model.setUpdateOperId(getCurrentUser().getOperId());
        Place cp = new Place();
        BeanUtils.copyProperties(cp, model);
        Integer result = placeService.updateById(cp);
        if (result > 0) {
            return new JsonCommon(SUCCESS_CODE);
        }
        return new JsonCommon(UPDATE_ERROR_CODE);
    }

    @AuthenticationAPI
    @ResponseBody
    @RequestMapping("/deleteById")
    public JsonCommon deleteById(Long placeId) throws BusinessException, InvocationTargetException, IllegalAccessException {
        if (placeId == null) {
            return new JsonCommon(PARAM_NOT_FOUND_CODE);
        }
        Integer result = placeService.deleteById(placeId);
        switch (result) {
            case -99:
                return new JsonCommon(DELETE_ERROR_CODE, "placeId 为" + placeId + " 的场所不存在");
            case -98:
                return new JsonCommon(DELETE_ERROR_CODE, "placeId 为" + placeId + " 的场所不存在场所类型，请联系管理员");
            case -1:
                return new JsonCommon(DELETE_ERROR_CODE, "有下级场所的场所不允许删除");
            case -2:
                return new JsonCommon(DELETE_ERROR_CODE, "已经关联视频的场所不允许删除");
            case -3:
                return new JsonCommon(DELETE_ERROR_CODE, "关联到园区的场所不允许删除");
            case -4:
                return new JsonCommon(DELETE_ERROR_CODE, "关联到公司的场所不允许删除");
            case -5:
                return new JsonCommon(DELETE_ERROR_CODE, "关联到食堂的场所不允许删除");
            case -6:
                return new JsonCommon(DELETE_ERROR_CODE, "关联到停车场的场所不允许删除");
            default:
                break;
        }
        if (result > 0) {
            return new JsonCommon(SUCCESS_CODE);
        }
        return new JsonCommon(DELETE_ERROR_CODE);
    }


}
